package com.yidu.t299.controller.twj;

import com.yidu.t299.contant.CommonConstant;
import com.yidu.t299.contant.SystemCode;
import com.yidu.t299.domain.twj.SysUser;
import com.yidu.t299.service.twj.SysUserBiz;
import com.yidu.t299.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author twj
 * @version 1.0.0
 */

@RestController
public class SysUserController {
    @Autowired
    private SysUserBiz sysUserBiz;

    /**
     * 新增
     * @param sysUser 用户对象
     * @return 字符串结果
     */
    @PostMapping("sysUser/add")
    public Result add(@RequestBody SysUser sysUser){
        sysUserBiz.insert(sysUser);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("sysUser/edit")
    public Result edit(@RequestBody SysUser sysUser) {
        sysUserBiz.update(sysUser);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("sysUser/remove/{usrId}")
    public Result remove(@PathVariable Integer usrId) {
        sysUserBiz.deleteByPrimaryKey(usrId);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("sysUser/remove")
    public Result removeBatch(Integer[] array) {
        sysUserBiz.deleteBatch(array);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("sysUser/find/{usrId}")
    public Result findById(@PathVariable Integer usrId) {
        return Result.success(sysUserBiz.selectByPrimaryKey(usrId));
    }

    /**
     * 分页查询
     */
    @GetMapping("sysUser/findPage")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                           SysUser sysUser) {
        return Result.success(sysUserBiz.findPage(pageNum, pageSize, sysUser));
    }

    /**
     * 判断用户名是否重复
     */
    @GetMapping("sysUser/findExists/{usrName}")
    public Result findUsernameExists(@PathVariable String usrName) {
        SysUser sysUser = sysUserBiz.selectByUsrName(usrName);
        if (sysUser == null) {
            return Result.success();
        }
        return Result.error(SystemCode.USERNAME_EXISTS.getCode(), SystemCode.USERNAME_EXISTS.getMessage(), null);
    }
}
